﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;



namespace CTEA_INDIA
{
    public partial class UserRegistration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;
           
            if (!IsPostBack)
            {
                PopulateTechType();
                BAL.Customers.PopulateLocation(ref ddlLocation);
                BAL.Customers.PopulateLocation(ref ddlEdtLoc);
            }

            if (!IsPostBack)
            {
                if (Session["UserID"] == null)
                {
                    Response.Redirect("Default.aspx");
                }

              
            }

            PopulateViewAllTechnical();
        }

        public void PopulateTechType()
        {
            DataSet ds = BAL.Customers.GetAllTechType();
            ddlTech.DataSource = ds;
            ddlTech.DataBind();
        }


        protected void IsNotFutureDate(object sender, ServerValidateEventArgs args)
        {
            DateTime dt;

            bool IsDate = DateTime.TryParse(args.Value.ToString(), out dt);

            if (IsDate)
            {
                if (dt.Date <= DateTime.Now.Date)
                    args.IsValid = true;
                else
                    args.IsValid = false;

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {


            string Email = txtEmailId.Text;

            int strVal = BAL.Customers.verifyuser(Email, "", 1);
            if (strVal == -1)
            {

                if (Page.IsValid)
                {
                    string EmailID = txtEmailId.Text;
                   // string Password = txtPass.Text;
                    string Pass = BAL.Encryption.CreateRandomPassword(8);
                    string EncriptedPassword = BAL.Encryption.Encript(Pass, "&%#@?,:*");
                   // string EncryptedPass = BAL.Encryption.Encript(Password, "&%#@?,:*");
                    string DecryptedPassword = BAL.Encryption.Decrypt(EncriptedPassword, "&%#@?,:*");
                    string Name = txtName.Text;
                    string Address = txtAddress.Text;
                    string Location = ddlLocation.SelectedValue ;


                    string mobileno = txtPers_MobileNo.Text;
                    DateTime dob = Convert.ToDateTime(txtDOB.Text);
                    int LoginAs = Convert.ToInt32(rbtloginas.SelectedValue);
                    string  TechID =  ddlTech.SelectedValue ;   
                    int UserID = BAL.Customers.TechRegistration(EmailID, EncriptedPassword, Address, Location, dob, LoginAs, Name, mobileno,TechID );


                    BAL.Customers.SendUserDetails(EmailID, Pass);
                    BAL.Customers.RegisterLink(EmailID, UserID.ToString(), EncriptedPassword, Name);
                   // spnEmptyDLMsg.Style.Add("display", "block");



                    Response.Redirect("confirmation.aspx");

                }


                spnMsg.Visible = true;
                spnMsg.InnerHtml = "Your Email ID already exist.";




            }



        }


        protected void rbtloginas_SelectedIndexChanged(object sender, EventArgs e)
        {
           
                if (rbtloginas.SelectedIndex == 0)
                {
                    ddlTech.Visible = false;
                    ddlTech.SelectedIndex = 0;
                }
                else if (rbtloginas.SelectedIndex == 1)
                {
                    ddlTech.Visible = true;
                }
                else
                {
                    rbtloginas.Visible = true;
                }
            
        }

        /// <summary>
        /// /////////////////////////////////////////////
        /// </summary>
        public void PopulateViewAllTechnical()
        {
            DataSet ds = BAL.Customers.ViewAllTechnical();
            //if (ds.Tables.Count > 0)
            //{
            //    if (ds.Tables[0].Rows.Count > 0)
            //    {
            gdTechnicalProfile.DataSource = ds;
            gdTechnicalProfile.DataBind();

            //}

            //else
            //{
            //spanerrormsg.Style.Add("display", "block");
            //spanerrormsg.InnerHtml = "Data not found";
            //    }
            //}

        }

        protected void gdTechnicalProfile_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lEdit = (LinkButton)e.Row.FindControl("lnkEdit");
                Button bUpdate = (Button)e.Row.FindControl("btnUpdate");
                LinkButton lDelete = (LinkButton)e.Row.FindControl("lnkDelete");
            
             

            }
        }


        protected void lnkEdit_Click(object sender, EventArgs e)
        {


            LinkButton lnkEdit = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkEdit.Parent.Parent;

            int ID = Convert.ToInt32(gdTechnicalProfile.DataKeys[row.RowIndex]["UserId"]);
            ViewState["ID"]=ID ;

            string[] details = BAL.Customers.GetTechnicalById(ID);


            txtNamee.Text = details[2];

            txtAdd.Text = details[4];

            txtMob.Text = details[6];

            txtDobirth.Text = DateTime.Parse( details[7]).ToShortDateString();

            txtEmail.Text = details[3];

            ddlEdtLoc.SelectedValue = details[5];

            //TextBox tTechType = (TextBox)gvPanel.FindControl("txtTechType");
            //tTechType.Text=details[1];
            lnkEdit_ModalPopupExtender.Show();

        }
        protected void lnkView_Click(object sender, EventArgs e)
        {
            LinkButton lnkVw = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkVw.Parent.Parent;


            int Cid = Convert.ToInt32(gdTechnicalProfile.DataKeys[row.RowIndex]["UserId"]);


            string[] Details = BAL.Customers.GetTechnicalById(Cid);

            lblAddress.Text = Details[4];
            lblAge.Text = Details[7];
            lblEmailId.Text = Details[3];
            lblLoctn.Text = Details[5];
            lblmobileno.Text = Details[6];
            lblName.Text = Details[2];
            lblTechtype.Text = Details[1];




            lnkView_ModalPopupExtender.Show();

            //document.getElementById("totalDaystd").innerHTML = "<asp:TextBox ID=\"txtDaysText\" runat=\"server\" ReadOnly=\"True\" Text=\""+ days +"\"></asp:TextBox>";

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {


            int UserId = Convert.ToInt32(ViewState["ID"]);
            


            string name = txtNamee.Text;

            string add = txtAdd.Text;

            string mob = txtMob.Text;

            DateTime dob = Convert.ToDateTime(txtDobirth.Text);

            string email = txtEmail.Text;

            string loc = ddlEdtLoc.Text;

            BAL.Customers.UpdateProfAdminTech(name, add, loc, dob, mob, UserId);

            PopulateViewAllTechnical();

        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
         
            LinkButton lnkEdit = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkEdit.Parent.Parent;


            int userId = Convert.ToInt32(gdTechnicalProfile.DataKeys[row.RowIndex ]["UserId"]);

            BAL.Customers.IsDeleteTechnical(userId);

            PopulateViewAllTechnical();

        }

        protected void gdTechnicalProfile_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdTechnicalProfile.PageIndex = e.NewPageIndex;
            PopulateViewAllTechnical();
        }

        

    }
}

